package com.ziyueyuwen.xiniao.util;

import org.apache.commons.lang3.time.DateUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author wancheng  on 2019/1/5.
 */
public class CalendarUtil {

    private static final Integer MONTH_SPRING = 3;
    private static final Integer MONTH_SUMMER = 6;
    private static final Integer MONTH_AUTUMN = 9;
    private static final Integer MONTH_WINTER = 12;
    /**
     * 周一至周日对应的值
     */
    private static final Integer WEEKDAY_MONDAY = 1;
    private static final Integer WEEKDAY_TUESDAY = 2;
    private static final Integer WEEKDAY_WEDNESDAY = 3;
    private static final Integer WEEKDAY_THURSDAY = 4;
    private static final Integer WEEKDAY_FRIDAY = 5;
    private static final Integer WEEKDAY_SATURDAY = 6;
    private static final Integer WEEKDAY_SUNDAY = 7;
    /**
     * 获取Calendar实例
     * @return
     */
    public static Calendar getCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 获取年
     * @return
     */
    public static Integer getYear(Date date) {
        if (date == null){
            return null;
        }
        return getCalendar(date).get(Calendar.YEAR);
    }

    /**
     * 获取月
     * @return
     */
    public static Integer getMonth(Date date) {
        return getCalendar(date).get(Calendar.MONDAY) + 1;
    }

    /**
     * 获取日
     * @return
     */
    public static Integer getDay(Date date) {
        return getCalendar(date).get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 是否是春季
     * @param date
     * @return
     */
    public static Boolean ifSpring(Date date) {
        Integer month = getMonth(date);
        return month <= MONTH_SPRING;
    }

    /**
     * 是否是夏季
     * @param date
     * @return
     */
    public static Boolean ifSummer(Date date) {
        Integer month = getMonth(date);
        return MONTH_SPRING < month && month <= MONTH_SUMMER;
    }

    /**
     * 是否是秋季
     * @param date
     * @return
     */
    public static Boolean ifAutumn(Date date) {
        Integer month = getMonth(date);
        return MONTH_SUMMER < month && month <= MONTH_AUTUMN;
    }

    /**
     * 是否是冬季
     * @param date
     * @return
     */
    public static Boolean ifWinter(Date date) {
        Integer month = getMonth(date);
        return MONTH_AUTUMN < month && month <= MONTH_WINTER;
    }

    /**
     * 获取2个日期之间的时间
     * @param before
     * @param after
     * @return
     */
    public static List<Date> getBetween(Date before, Date after) {
        Date beforeTime = DateUtils.truncate(before, Calendar.DAY_OF_MONTH);
        Date endTime = DateUtils.truncate(after, Calendar.DAY_OF_MONTH);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beforeTime);
        List<Date> res = new ArrayList<>();
        while (calendar.getTime().getTime() <= endTime.getTime()) {
            res.add(calendar.getTime());
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return res;
    }

    public static Date getWeekDay(Date date,Integer weekDay){
        Calendar calendar = getCalendar(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        int first = calendar.getFirstDayOfWeek();
        // 计算周五的日期
        calendar.add(Calendar.DAY_OF_WEEK, first - day + weekDay - 1);
        return calendar.getTime();
    }

    /**
     * 获取当周周二的时间
     * @param date
     * @return
     */
    public static Date getTuesday(Date date){
        return getWeekDay(date,WEEKDAY_TUESDAY);
    }

    public static void main(String[] args){
        System.out.println(DateUtil.date2Str(getWeekDay(DateUtil.str2Date("2019-04-15 00:00:00"),2)));
    }
}
